package com.myapp.db.config.define;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.myapp.data.define.BaseBean;

public class DBConfigContainer {
	private static DBConfigContainer dbConfigContainer = null;

	// 以表名为key的map，便于双向查询
	private Map<String, DBConfigBean> tableIndexMap = new HashMap<String, DBConfigBean>();

	// 以entity的名字为key的map，便于双向查询
	private Map<Class<? extends BaseBean>, DBConfigBean> entityIndexMap = new HashMap<Class<? extends BaseBean>, DBConfigBean>();

	private DBConfigContainer() {
	}

	public static DBConfigContainer getInstance() {
		if (dbConfigContainer == null) {
			dbConfigContainer = new DBConfigContainer();
		}
		return dbConfigContainer;
	}

	/**
	 * 根据表名获取数据库配置bean
	 * 
	 * @param tableName
	 *            表名
	 * 
	 * @return 数据库配置bean
	 */
	public DBConfigBean getDBConfigBeanByTableName(String tableName) {
		return this.tableIndexMap.get(tableName);
	}

	public DBConfigBean getDBConfigBeanByEntity(Class<? extends BaseBean> entity) {
		return this.entityIndexMap.get(entity);
	}

	/**
	 * 添加一个数据库配置bean
	 * 
	 * @param dbConfigBean
	 *            数据库配置bean
	 */
	public void addDBConfigBean(DBConfigBean dbConfigBean) {
		this.entityIndexMap.put(dbConfigBean.getEntityClass(), dbConfigBean);
		this.tableIndexMap.put(dbConfigBean.getName(), dbConfigBean);
	}

	/**
	 * 获取所有的数据库配置信息
	 * 
	 * @return 数据库配置信息
	 */
	public Collection<DBConfigBean> getAllDBConfigBean() {
		return this.tableIndexMap.values();
	}

	public void clear() {
		this.entityIndexMap.clear();
		this.tableIndexMap.clear();
	}
}
